﻿@model CategoryModel
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

@Html.SmartStore().TabStrip().Name("category-edit").OnAjaxSuccess("categoryEditTab_onAjaxSuccess").Style(TabsStyle.Material).Position(TabsPosition.Left).Items(x => {

	x.Add().Text(T("Admin.Catalog.Categories.Info").Text).Icon("fa fa-pencil-alt fa-lg fa-fw").Content(TabInfo()).Selected(true);
	x.Add().Text(T("Admin.Common.SEO").Text).Icon("fa fa-search fa-lg fa-fw").Content(TabSeo());
	x.Add().Text(T("Admin.Catalog.Categories.Products").Text).Icon("fa fa-cube fa-lg fa-fw").Content(TabProducts());

	EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "category-edit", this.Html, this.Model));
})

@helper TabInfo()
{
		<table class="adminContent mb-2">
			@if (Model.Id != 0)
			{
            <tr>
				<td class="adminTitle">
					@Html.SmartLabelFor(model => model.Id)
				</td>
				<td class="adminData">
					@Html.TextBoxFor(model => model.Id, new { @readonly = "readonly", @class = "form-control-plaintext" })
					@Html.ValidationMessageFor(model => model.Id)
				</td>
			</tr>
			}
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.ExternalLink)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.ExternalLink)
                    @Html.ValidationMessageFor(model => model.ExternalLink)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.ParentCategoryId)
                </td>
                <td class="adminData">
				    @Html.DropDownListFor(model => model.ParentCategoryId,
				    Model.ParentCategoryId == null ? Enumerable.Empty<SelectListItem>() : new List<SelectListItem> {
					    new SelectListItem { Text = Model.ParentCategoryBreadcrumb, Value = Model.ParentCategoryId.ToString() }
				    },
				    new
				    {
					    placeholder = "[" + T("Common.Unspecified") + "]",
					    data_select = true,
					    data_select_lazy = true,
					    data_select_init_text = Model.ParentCategoryBreadcrumb,
					    data_select_url = Url.Action("AllCategories", "Category", new { selectedIds = Model.ParentCategoryId })
				    })
                    @Html.ValidationMessageFor(model => model.ParentCategoryId)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.DisplayOrder)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.DisplayOrder)
                    @Html.ValidationMessageFor(model => model.DisplayOrder)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Published)
                </td>
                <td class="adminData">
                    @Html.EditorFor(x => x.Published)
                    @Html.ValidationMessageFor(model => model.Published)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.ShowOnHomePage)
                </td>
                <td class="adminData">
                    @Html.EditorFor(x => x.ShowOnHomePage)
                    @Html.ValidationMessageFor(model => model.ShowOnHomePage)
                </td>
            </tr>
		</table>

    @(Html.LocalizedEditor<CategoryModel, CategoryLocalizedModel>("category-info-localized",
		@<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].Name)
                </td>
                <td class="adminData">
					@*IMPORTANT: Do not delete, this hidden element contains the id to assign localized values to the corresponding language *@
					@Html.HiddenFor(model => model.Locales[item].LanguageId)

                    @Html.EditorFor(model => Model.Locales[item].Name)
                    @Html.ValidationMessageFor(model => model.Locales[item].Name)
                </td>
            </tr>
			<tr>
				<td class="adminTitle">
				 	@Html.SmartLabelFor(model => model.Locales[item].FullName)
				</td>
				<td class="adminData">
				 	@Html.EditorFor(model => Model.Locales[item].FullName)
				 	@Html.ValidationMessageFor(model => model.Locales[item].FullName)
				</td>
			</tr>
            <tr class="description-container">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].Description)
                </td>
                <td class="adminData wide">
                    @Html.EditorFor(model => model.Locales[item].Description, "Html")
                    @Html.ValidationMessageFor(model => model.Locales[item].Description)
                </td>
            </tr>
            <tr class="description-container d-none">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].BottomDescription)
                </td>
                <td class="adminData wide">
                    @Html.EditorFor(model => model.Locales[item].BottomDescription, "Html")
                    @Html.ValidationMessageFor(model => model.Locales[item].BottomDescription)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                </td>
                <td class="adminData">
					<button type="button" class="btn btn-secondary description-toggle-button">
						<i class="fa fa-exchange-alt"></i>
						<span>@T("Admin.Catalog.Categories.DescriptionToggle")</span>
					</button>
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].BadgeText)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => Model.Locales[item].BadgeText)
                    @Html.ValidationMessageFor(model => model.Locales[item].BadgeText)
                </td>
            </tr>
        </table>
		,
		@<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Name)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.FullName)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.FullName)
                    @Html.ValidationMessageFor(model => model.FullName)
                </td>
            </tr>
            <tr class="description-container">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Description)
                </td>
                <td class="adminData wide">
                    @Html.EditorFor(x => x.Description, "Html")
                    @Html.ValidationMessageFor(model => model.Description)
                </td>
            </tr>
            <tr class="description-container d-none">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.BottomDescription)
                </td>
                <td class="adminData wide">
                    @Html.EditorFor(x => x.BottomDescription, "Html")
                    @Html.ValidationMessageFor(model => model.BottomDescription)
                </td>
            </tr>
			<tr>
				<td class="adminTitle"></td>
				<td class="adminData">
					<button type="button" class="btn btn-secondary description-toggle-button">
						<i class="fa fa-exchange-alt"></i>
						<span>@T("Admin.Catalog.Categories.DescriptionToggle")</span>
					</button>
				</td>
			</tr>
                <tr>
                    <td class="adminTitle">
                        @Html.SmartLabelFor(model => model.BadgeText)
                    </td>
                    <td class="adminData">
                        @Html.EditorFor(model => model.BadgeText)
                        @Html.ValidationMessageFor(model => model.BadgeText)
                    </td>
                </tr>
        </table>
	))

    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.BadgeStyle)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.BadgeStyle, Model.AvailableBadgeStyles)
                @Html.ValidationMessageFor(model => model.BadgeStyle)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.PictureId)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.PictureId, new { transientUpload = true })
                @Html.ValidationMessageFor(model => model.PictureId)
            </td>
        </tr>
        <tr>
            <td colspan="2"><hr /></td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.DefaultViewMode)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.DefaultViewMode, Model.AvailableDefaultViewModes, T("Common.Unspecified"))
                @Html.ValidationMessageFor(model => model.DefaultViewMode)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.AllowCustomersToSelectPageSize)
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.AllowCustomersToSelectPageSize)
                @Html.ValidationMessageFor(model => model.AllowCustomersToSelectPageSize)
            </td>
        </tr>
        <tr id="pnlPageSize">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.PageSize)
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.PageSize)
                @Html.ValidationMessageFor(model => model.PageSize)
            </td>
        </tr>
        <tr id="pnlPageSizeOptions">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.PageSizeOptions)
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.PageSizeOptions)
                @Html.ValidationMessageFor(model => model.PageSizeOptions)
            </td>
        </tr>
        <tr>
            <td colspan="2"><hr /></td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Alias)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Alias)
                @Html.ValidationMessageFor(model => model.Alias)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.CategoryTemplateId)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.CategoryTemplateId, Model.AvailableCategoryTemplates)
                @Html.ValidationMessageFor(model => model.CategoryTemplateId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SelectedDiscountIds)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SelectedDiscountIds)
                @Html.ValidationMessageFor(model => model.SelectedDiscountIds)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SelectedStoreIds)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SelectedStoreIds)
                @Html.ValidationMessageFor(model => model.SelectedStoreIds)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SelectedCustomerRoleIds)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SelectedCustomerRoleIds)
                @Html.ValidationMessageFor(model => model.SelectedCustomerRoleIds)

	            <div class="mt-4">
		            <div class="alert alert-danger">
			            @T("Admin.Configuration.Category.Acl.AssignToSubCategoriesAndProducts.Hint")
		            </div>
		            <a href="@Url.Action("InheritAclIntoChildren", "Category", new { categoryId = Model.Id })" name="inherit-acl-into-children" class="btn btn-danger">
			            <i class="fa fa-bolt"></i>
			            <span>@T("Admin.Configuration.Category.Acl.AssignToSubCategoriesAndProducts")</span>
		            </a>
	            </div>
            </td>
        </tr>
		@if (Model.CreatedOn.HasValue)
		{
			<tr>
				<td class="adminTitle">
					@Html.SmartLabelFor(model => model.CreatedOn)
				</td>
				<td class="adminData">
					<div class="form-control-plaintext">
						@Html.DisplayFor(model => model.CreatedOn)
					</div>
				</td>
			</tr>
		}
		@if (Model.UpdatedOn.HasValue)
		{
			<tr>
				<td class="adminTitle">
					@Html.SmartLabelFor(model => model.UpdatedOn)
				</td>
				<td class="adminData">
					<div class="form-control-plaintext">
						@Html.DisplayFor(model => model.UpdatedOn)
					</div>
				</td>
			</tr>
		}
    </table>
}

@helper TabSeo()
{
    @(Html.LocalizedEditor<CategoryModel, CategoryLocalizedModel>("category-seo-localized",
		@<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].MetaKeywords)
                </td>
                <td class="adminData">
					@*IMPORTANT: Do not delete, this hidden element contains the id to assign localized values to the corresponding language *@
					@Html.HiddenFor(model => model.Locales[item].LanguageId)

                    @Html.TextBoxFor(model => model.Locales[item].MetaKeywords)
                    @Html.ValidationMessageFor(model => model.Locales[item].MetaKeywords)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].MetaDescription)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(model => model.Locales[item].MetaDescription)
                    @Html.ValidationMessageFor(model => model.Locales[item].MetaDescription)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].MetaTitle)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(model => model.Locales[item].MetaTitle)
                    @Html.ValidationMessageFor(model => model.Locales[item].MetaTitle)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].SeName)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.Locales[item].SeName)
                    @Html.ValidationMessageFor(model => model.Locales[item].SeName)
                </td>
            </tr>
        </table>
		,
		@<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.MetaKeywords)
                </td>
                <td class="adminData">
                    @Html.TextBoxFor(x => x.MetaKeywords)
                    @Html.ValidationMessageFor(model => model.MetaKeywords)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.MetaDescription)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(x => x.MetaDescription)
                    @Html.ValidationMessageFor(model => model.MetaDescription)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.MetaTitle)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(x => x.MetaTitle)
                    @Html.ValidationMessageFor(model => model.MetaTitle)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.SeName)
                </td>
                <td class="adminData">
					@Html.EditorFor(x => x.SeName)
					@if (Model.Id != 0)
					{
						@Html.Action("NamesPerEntity", "UrlRecord", new { entityName = "Category", entityId = Model.Id })
					}
					@Html.ValidationMessageFor(model => model.SeName)
                </td>
            </tr>
        </table>
    ))
}

@helper TabProducts()
{
	if (Model.Id > 0)
	{
        <div class="adminContent">
            <div class="adminRow">
                <div class="adminTitle">
                    @Html.SmartLabelFor(model => model.SelectedRuleSetIds)
                </div>
                <div class="adminData">
                    @Html.EditorFor(model => model.SelectedRuleSetIds)
                    @Html.ValidationMessageFor(model => model.SelectedRuleSetIds)
                </div>
            </div>
        </div>
        if (Model.SelectedRuleSetIds.Any())
        {
            <div class="alert alert-info">
                @T("Admin.Catalog.Categories.ProductListDescription")
            </div>
        }
		<div>
			@(Html.Telerik().Grid<CategoryModel.CategoryProductModel>()
                .Name("products-grid")
                .DataKeys(keys => { keys.Add(x => x.Id); })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Ajax()
                        .Select("ProductList", "Category", new { categoryId = Model.Id })
                        .Update("ProductUpdate", "Category")
                        .Delete("ProductDelete", "Category");
                })
                .Columns(columns =>
                {
                    columns.Bound(x => x.ProductName)
                        .ReadOnly()
                        .ClientTemplate(Html.LabeledProductName("ProductId", "ProductName"));
                    columns.Bound(x => x.Sku)
                        .ReadOnly();
                    columns.Bound(x => x.Published)
                        .ReadOnly()
                        .ClientTemplate(Html.SymbolForBool("Published"))
                        .Centered();
                    columns.Bound(x => x.IsFeaturedProduct)
                        .ClientTemplate(Html.SymbolForBool("IsFeaturedProduct"))
                        .Centered();
                    columns.Bound(x => x.DisplayOrder1)
                        .Centered();
			        columns.Bound(x => x.IsSystemMapping)
                        .ReadOnly()
					    .ClientTemplate(Html.SymbolForBool("IsSystemMapping"))
					    .Centered();
                    columns.Command(commands =>
                    {
                        commands.Edit().Localize(T);
                        commands.Delete().Localize(T);
                    })
                    .HtmlAttributes(new { align = "right" });
                })
                .ToolBar(commands => commands.Template(CategoryProductsGridCommands))
                .Pageable(settings => settings.PageSize(Model.GridPageSize).Position(GridPagerPosition.Both))
                .PreserveGridState()
                .EnableCustomBinding(true))
		</div>
	}
	else
	{
		<div class="alert alert-warning">
			@T("Admin.SaveBeforeEdit")
		</div>
	}
}

@helper CategoryProductsGridCommands(Grid<CategoryModel.CategoryProductModel> grid)
{
	@(Html.SmartStore().EntityPicker()
		.IconCssClass("fa fa-plus")
		.HtmlAttribute("class", "t-button t-grid-add")
		.Caption(T("Admin.Catalog.Categories.Products.AddNew").Text.EncodeJsString('\'', false))
		.OnSelectionCompleted("PickProducts_Completed"))

    if (Model.ShowRuleApplyButton)
    {
	    <button type="button" id="btnReapplyRules" class="t-button t-grid-save-changes" data-url="@Url.Action("ApplyRules", new { id = Model.Id })" data-ask="@T("Admin.Common.AskToProceed")">
		    <i class="fa fa-play"></i>
		    <span>@T("Admin.Rules.ReapplyRules")</span>
	    </button>
    }
}

<script>
	function categoryEditTab_onAjaxSuccess(tab, pane) {
		applyCommonPlugins(pane);
	}

	$(function () {
	    $("#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)").change(togglePageSize);
	    togglePageSize();
	
		// Toggle top\bottom description.
		$('.description-toggle-button').click(function() {
			$('.description-container').toggleClass("d-none");
        });

        // Reapply rules.
        $('#btnReapplyRules').on('click', function () {
    		$(this).doPostData({ });
        });
	});

	function PickProducts_Completed(ids) {
		$({}).doAjax({
			type: 'POST',
			url: '@Url.Action("ProductAdd")',
			data: { "categoryId": @Model.Id, "selectedProductIds": ids },
			callbackSuccess: function () {
				$('#products-grid').data('tGrid').ajaxRequest();
			}
		});
		return true;
	}

    function togglePageSize() {
        var isEnabled = $('#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)').val();

        if (isEnabled != "false") {
            $('#pnlPageSize').hide();
            $('#pnlPageSizeOptions').show();
        } else {
            $('#pnlPageSize').show();
            $('#pnlPageSizeOptions').hide();
        }
    }
</script>
